/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Threading;
using Whitemice.ZOGI.RPC2;
using Whitemice.ZOGI.Backend;
using System.Collections.Generic;
using CookComputing.XmlRpc;

namespace Whitemice.ZOGI
{
  public abstract partial class Entity : ZOGIDecoder
  {   
    private static int[] Levels = { 1, 2, 4, 8, 16, 32, 64, 128,
		                                256, 512, 1024, 2048, 4096,
		                                16384, 32768 };
		                                
		protected Dictionary<string,List<int>> subordinateIndex;
    
    public Entity(IDictionary<string,object> content, int d)
    { 
      InitSubordinates();
      Take(content, d);
      this.ZLOG(string.Format("{0}#{1} initialized", this.EntityName, this.ObjectId));
    }
    
    public Entity()
    {
      InitSubordinates();
      SetObjectId(Server.InternalId);
    }
    
		internal virtual void Take(IDictionary<string,object> x, int d)
		{
			if (x.ContainsKey("objectId")) SetObjectId(AsInt32(x, "objectId"));
				else SetObjectId(Server.InternalId);
			SetVersion(AsInt32(x, "version"));
			SetEntityName(AsString(x, "entityName"));
			if (x.ContainsKey("FLAGS")) SetHints(AsArrayOfStrings(x, "FLAGS"));
				else SetHints(new string[] {});
		  SetDetailLevel(DetailLevel | d);
		} // end Take

    public IEnumerable<ObjectLink> Links
    {
      get
      {
        return Server.Cache.GetChildrenOf<ObjectLink>(ObjectId);
      }
    }
    
	  public bool IsLevel(int d)
		{
		  if (FirstClass)
		  {
		  	if (d == 0) return true;
		  	if (DetailLevel == 65535) return true;
        foreach(int level in Levels)
        {
          if ((d & level) == level)
          {
            if ((DetailLevel & level) == level)
              continue;
            return false;
          } // end if-level-requested
        } // end foreach
      }
			return true;
		} // end DetailLevelOk
    
    public virtual bool FirstClass { get { return false; } }
    
    protected int _detailLevel = 0;
    protected void SetDetailLevel(int d) 
    {
      if (this.FirstClass)
        _detailLevel = d; 
    }
    public int DetailLevel 
    { 
      get 
      { 
        if (FirstClass)
          return _detailLevel; 
        return 65535;
      } 
    }
    
    protected int _objectId;
    protected void SetObjectId(int id) { _objectId = id; }
    public int ObjectId { get { return _objectId; } }
    
    protected string _entityName;
    protected void SetEntityName(string e) { _entityName = e; }
    public string EntityName { get { return _entityName; } }
    
    protected int _parentObjectId = 0;
    protected void SetParentObjectId(int id) { _parentObjectId = id; }
    public int ParentObjectId { get { return _parentObjectId; } }

    protected int _version = -1;
    protected void SetVersion(int v) { _version = v; }
    public int Version { get { return _version; } }
    
    protected string[] _hints;
    protected void SetHints (string[] h) { _hints = h; }
    protected string[] Hints { get { return _hints; } }
    public bool HasHint(string hint)
    {
      
      foreach(string h in Hints)
        if (hint.ToUpper().Equals(h)) return true;
      return false;
    } // end HasHint
    
    public Entity Parent 
    { 
      get 
      { 
        return Server.Connection.Get(ParentObjectId, 65535, true); 
      } 
    } // end Parent
		
		public virtual bool CanModify { get { return false; } }
		public virtual bool CanDelete { get { return false; } }
		public virtual bool CanRead { get { return true; } }
		
		public IEnumerable<Whitemice.ZOGI.ObjectProperty> Properties
		{
		  get
		  {
		    return Server.Cache.GetChildrenOf<ObjectProperty>(ObjectId);
		  }
		}
				
		public virtual XmlRpcStruct Encode()
		{
		  XmlRpcStruct result;
		  
		  result = new XmlRpcStruct();
		  return result;
		}
  }
}
